package depthtag;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;

import javax.swing.SwingUtilities;

import depthtag.display.Viewer;


public class MainClass {

	LogParser lp = null;
	Viewer application = null;
	boolean noCopy;
	boolean metric = true;
	BlockingQueue<Task> q  = new ArrayBlockingQueue<Task>(1);
	public MainClass(boolean noCopy) {
		lp = new LogParser(0);
		this.noCopy = noCopy;
		final MainClass callback = this;
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				application = new Viewer(callback);
			}
		});
		
		try {
			while (true) {
				doTask(q.take()); 
			}
		} catch (InterruptedException ex) {}
		
//		Executors.callable(task)
	}
	
	private void doTask(Task task){
		switch (task.getTypeOfTask()) {
		case Task.MATCH_PICTURES:
			matchPictures(task.getPathToExifTool(), task.getPictureFolder());
			break;
		case Task.TEST_EXIF_TOOL:
			testExifTool(task.getPathToExifTool());
			break;
		case Task.UPDATE_PICTURES:
			updatePictures(task.getPathToExifTool(), task.getPictureFolder());
			break;
		case Task.PARSE_LOGS:
			parseLogs(task.getLogFolder());
			break;
		default:
			break;
		}
	}
	
	public void addTask(Task task){
		q.add(task);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		boolean noCopy = false;
		for (String string : args) {
			if(string.equals("no-copy")){
				noCopy = true;
			}
		}
		new MainClass(noCopy);
	}
	
	private void parseLogs(String logFolder){
//		System.out.println("Parsing log folder=" + logFolder);
		lp.readFolder(logFolder, application);
	}
	
	private void testExifTool(String pathToExifTool){
		try {
			ExifToolWrapper etw = new ExifToolWrapper(new File(pathToExifTool));
			String version = etw.getVersion();
			if(version == null){
				application.addStatusText("ExifTool not found!");
			}
			else{
				application.addStatusText("ExifTool found, version " + version);
			}
		} catch (FileNotFoundException e) {
			application.addStatusText(e.getMessage());
		}
	}
	
	
	private void matchPictures(String pathToExifTool, String pictureFolder){
		Map<String, Double> timeStamps = lp.getTimeStamps();
		if(pathToExifTool != null){
			PictureParser pp = null;
			try {
				pp = new PictureParser(pathToExifTool,pictureFolder);
			} catch (FileNotFoundException e) {
				application.addStatusText("Exiftool location not set");
			}
			if(pp != null){
				Map<File, Map<String, String>> result = pp.getTimestamps(application);
				int nrOfMatches = 0;
				if(result != null){
					for (Entry<File, Map<String, String>> entry : result.entrySet()) {
						File file = entry.getKey();
						String createDated = entry.getValue().get(PictureParser.CREATED_DATE);
						if(createDated != null){
							createDated = createDated.substring(0,19);
						}
						Double depth = timeStamps.get(createDated);
						if(depth != null){
							application.addStatusText("Found a match! File=" + file.getName() + " depth=" + depth);
							nrOfMatches++;
	//						System.out.println("Found a match! File=" + file.getName() + " depth=" + depth);
						}
						else{
	//						application.addStatusText("No timematch for " + file.getName() + " " + createDated);
						}
					}
					application.addStatusText("Out of " + result.size() + " pictures, " + nrOfMatches + " matches were found.\n");
				}
				else{
					application.addStatusText(pictureFolder + " is not a folder. Please make sure \"Location of pictures\" is a folder"); 
				}
			}
		}
	}
	
	private void updatePictures(String pathToExifTool, String pictureFolder){
		Map<String, Double> timeStamps = lp.getTimeStamps();
		if(pathToExifTool != null){
			PictureParser pp = null;
			try {
				pp = new PictureParser(pathToExifTool,pictureFolder);
			} catch (FileNotFoundException e) {
				application.addStatusText("Exiftool location not set");
			}
			if(pp != null){
				Map<File, Map<String, String>> result = pp.getTimestamps(application);
				
				for (Entry<File, Map<String, String>> entry : result.entrySet()) {
					File file = entry.getKey();
					
					String createDated = entry.getValue().get(PictureParser.CREATED_DATE);
					if(createDated != null){
						createDated = createDated.substring(0,19);
					}
					Double depth = timeStamps.get(createDated);
					if(depth != null){
						application.addStatusText("Saving file " + file.getName() + " depth=" + depth);
						pp.addDepthToImage(file, depth, noCopy, metric);
					}
				}
			}
		}
	}
	
	public void setMetric(boolean isMetric){
		this.metric = isMetric;
	}
}